import axios from "axios";
import { ElMessage } from "element-plus";
import router from '../router/index'
const BASE_URL = 'http://192.168.124.27:5000'
export default {
    // get方法封装
    get(url, params, config, headers) {
        config = config ? config : {}
        headers = headers ? headers : {}
        return new Promise((resolve, reject) => {
            this.request(url, 'get', params, config, headers).then(res => {
                resolve(res)
            }).catch(err=>{
                reject(err)
            })
        })
    },
    // post方法封装
    post(url, params, config, headers) {
        config = config ? config : {}
        headers = headers ? headers : {}
        return new Promise((resolve, reject) => {
            this.request(url, 'post', params, config, headers).then(res => {
                resolve(res.data)
            }).catch(err=>{
                reject(err)
            })
        })
    },
    /* 文件下载 */
    download(url, params, config, headers) {
        config = config ? config : { responseType: 'blob' }
        headers = headers ? headers : {}
        return new Promise((resolve, reject) => {
            this.request(url, 'post', params, config, headers).then(res => {
                const fileType = res.headers['content-disposition'].split('.')[1]
                if(res.status == 200){
                    resolve({code:200, fileType: `.${fileType}`, data: res.data })
                }else{
                    resolve({ code: -1, msg:"下载失败"})
                }
            }).catch(err=>{
                reject(err)
            })
        })
    },
    // axios请求封装
    /**
     * @url 请求路径
     * @method  请求方式
     * @baseUrl 基础路径拼接到url前
     * @params  参数
     */
    request(url, method, params, config, headers) {
        // console.log(process.env);
        return new Promise((resolve, reject) => {
            // 请求拦截器
            axios.interceptors.request.use(config=>{
                // if(document.cookie.token || ){
                //     headers['proveToken'] = document.cookie.token.proveToken
                // }
                return config;
            },error=>{
                return Promise.reject(error);
            });
            // 请求携带token
            config['timeout'] = process.env.VUE_APP_timeout,
            headers['g-token'] = sessionStorage.getItem('token') || ''
            headers['g-session'] = sessionStorage.getItem('session') || ''
                // headers['Access-Control-Allow-Origin'] = '*'
            axios({
                url: url,
                method: method,
                baseURL: process.env.VUE_APP_API_URL || BASE_URL ,
                data: params,
                headers: headers,
                ...config
            }).then(res => {
                // console.log('res', res);
                resolve(res)
            }).catch(err => {
                if(err.response?.status){
                    if (err.response.status == 401){
                        ElMessage({
                            message: '登录已过期,请重新登录',
                            type: "error",
                        });
                        router.push({ name: 'login' })
                    }
                    if (err.response.status == 402){
                        ElMessage({
                            message: '缺少token',
                            type: "error",
                        });
                        router.push({ name: 'login' })
                    }
                }
                reject(err)
            })
        })
    }
}